/**
 * webapi
 * @authors 杨兴洲（of2502）
 * @date    2017/5/22 0022 17:07
 * @version 1.0
 * @last-modify  杨兴洲（of2502）
 */

import QMFetch from 'fetch/index';
import {QMConst} from 'qm-ux';

/**
 * 账户相关api
 */
const WebApi = {

    /**
     * 查询用户的公众号信息
     */
    fetchUserInfo: () => QMFetch({
        url: 'api/authorizer/info',
        method: 'GET'
    }),

    /**
     * 获取授权链接地址
     */
    fetchAuthUrl: () => {
        // let host = QMConst.HOST[QMConst.PROJECT.APIHost];
        // if (host.indexOf("//") === 0) {
        //     host = location.protocol + host
        // }
        let host = location.protocol + "//" + location.host;
        return QMFetch({
            url: `api/authorization/url`,
            method: 'GET',
            body: {
                redirect_uri: encodeURIComponent(`${host}/callback.html`)
            }
        })
    },

    /**
     * 绑定用户信息
     */
    bindAccount: (authCode) => QMFetch({
        url: 'api/authorizer/bind',
        method: 'POST',
        body: {authCode}
    }),

    /**
     * 重置API调用次数
     * {{host}}/api/authorizer/reset
     */
    resetAPICount: (authCode) => QMFetch({
        url: 'api/authorizer/reset',
        method: 'POST',
    }),
};

export default WebApi;